#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define pb push_back
#define endl '\n'
#define all(v) v.begin(), v.end()
#define fr(m,n) for(i=m;i<n;i++)
#define frr for(i=n-1;i>=0;i--)
#define ff for(i=0;i<n;i++)
#define ff1 for(i=1;i<=n;i++)
#define frj(m,n) for(j=m;j<n;j++)
#define frl(m,n) for(l=m;l<n;l++)
#define frd(m,n) for(d=m;d<n;d++)
#define wq while(q--)
#define lb lower_bound
#define ub upper_bound
#define yes cout << "YES"
#define no cout << "NO"
#define yesm cout << "Yes"
#define nom cout << "No"
#define inf INT_MAX
#define ext {cout << -1; return;}
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define rall(a) (a).rbegin(),(a).rend()
#define vi vector<int>
#define pi pair<int,int>
#define vvi vector<vi>
#define vc vector<char>
#define vs vector<string>
#define ss set<string>
#define vpi vector<pi>
#define mii map<int,int>
#define mci map<char,int>
#define als(i) cout << i.fi << " " << i.se << endl
#define si set<int>
#define sc set<char>
#define mp make_pair
#define fi first
#define se second
#define sz size()
#define el cout<<endl
bool id = 0, id1 = 0, id2 = 0;
int x = 0, y = 0, z = 0;
int ans = 0, num = 0, sum = 0, mo = 0, me = 0, cnt = 0, mi = inf, ma = 0, mid;
string s, p, st;
int h = 0, w = 0, n = 0, m = 0, t = 0, k = 0, i = 0, j = 0, l = 0, r = 0, q = 0, a = 0, b = 0, c = 0, d = 0;
const int mod = 1e9 + 7;
// const int mod = 998244353;
const double PI = 3.141592653589793238462643383279502884;
const long long N = 2e5 + 2;
// int add(int x, int y) {return ((x + y) % mod + mod) % mod;}
// int mul(int x, int y) {return x * y % mod;}
// long long binpow(long long a, long long b) {a %= mod; long long res = 1; while (b > 0) {if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1;} return res;}
// int inv(int x) {return binpow(x, mod - 2);}
// int divide(int x, int y) {return mul(x, inv(y));}
// ll factorial(ll n) {ll ans = 1; for (int i = 1; i <= n; ++i) {ans = ans * i;} return ans;}
// bool check_prime(ll n) {if (n <= 1)return false; if (n <= 3)return true; if (n % 2 == 0 || n % 3 == 0)return false; for (int i = 5; i * i <= n; i = i + 6)if (n % i == 0 || n % (i + 2) == 0)return false; return true;}
int bintoint(string s) {reverse(all(s)); int xx = 0; int m = 1; frj(0, s.sz) {xx += (s[j] == '1' ? m : 0); m <<= 1;} return xx;}
string inttobin(int k) {string p; while (k != 0) {p += (k & 1) + 48; k >>= 1;} return p;}
// int nCr(int n, int r) {if (n < r) return 0; if (r == 0) return 1; int mm = (fac[r] * fac[n - r]); mm %= mod; k = divide(fac[n], mm); k %= mod; return k;}
vi put(vi ad, int i, int j) {
int n = ad.sz;
vi vw = {ad.begin() + i, ad.begin() + j + 1};
reverse(all(vw));
// vi w = {v.begin(), v.begin() + j + 1};
// vi ww = {v.begin() + j + 1, v.end()};
vi has;
// for (auto i : w)has.pb(i);
for (auto i : vw)has.pb(i);
// for (auto i : ww)has.pb(i);
return has;
}
void solve() {
id = 0, id1 = 0, id2 = 0;
x = 0, y = 0, z = 0, cnt = -1;
ans = 0, num = 0, sum = 0, mo = 0, me = 0, cnt = 0, mi = inf, ma = 0;
s = "", p = "", st = "";
n = 0, m = 0, k = 0, i = 0, j = 0, l = 0, r = 0, q = 0, a = 0, b = 0, c = 0, d = 0;
// cin >> n >> a >> b;
// if (n < a) {
// cout << 0;
// }
// else {
// ans = n - n / a;
// cout << ans;
// }
cin >> n >> s; vi ki, ka;
ff{
if (s[i] == '(') ka.pb(i);
else ki.pb(i);
}
if (ka.sz == ki.sz) {
// for (auto i : ka)cout << i << " "; el;
j = 0;
n = ki.sz;
// k = 2 * n + 1;
ff{
if (ka[i] > ki[i]) ans++;
}
cout << ans*2;
}
else ext;
}
// int fac[N];
// vi pfact[N];
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
int T = 1;
// cin >> T;
// vector <ll> vec;
// bool prime[10005];
// memset(prime, true, sizeof(prime));
// for (ll i = 2; i <= 10000; i++) {
// if (prime[i]) {
// vec.push_back(i);
// for (ll j = i * i; j <= 10000; j += i) {
// prime[j] = false;
// }
// }
// }
// for (int i = 2; i < N; i++)
// {
// if (pfact[i].size() == 0)
// {
// for (int j = i; j < N; j += i)
// pfact[j].pb(i);
// }
// }
// fac[0] = 1;
// fr(1, N) {
// fac[i] = fac[i - 1] * i % mod;
// }
while (T--) {
solve();
cout << endl;
} return 0;
}
1302. Deepest Leaves Sum | 1209. Remove All Adjacent Duplicates in String II |
994. Rotting Oranges | 983. Minimum Cost For Tickets |
973. K Closest Points to Origin | 969. Pancake Sorting |
967. Numbers With Same Consecutive Differences | 957. Prison Cells After N Days |
946. Validate Stack Sequences | 921. Minimum Add to Make Parentheses Valid |
881. Boats to Save People | 497. Random Point in Non-overlapping Rectangles |
528. Random Pick with Weight | 470. Implement Rand10() Using Rand7() |
866. Prime Palindrome | 1516A - Tit for Tat |
622. Design Circular Queue | 814. Binary Tree Pruning |
791. Custom Sort String | 787. Cheapest Flights Within K Stops |
779. K-th Symbol in Grammar | 701. Insert into a Binary Search Tree |
429. N-ary Tree Level Order Traversal | 739. Daily Temperatures |
647. Palindromic Substrings | 583. Delete Operation for Two Strings |
518. Coin Change 2 | 516. Longest Palindromic Subsequence |
468. Validate IP Address | 450. Delete Node in a BST |